package org.eclipse.soc.ide4edu.assignment.wizards;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.soc.ide4edu.assignment.operations.AssignmentCreationOperation;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.wizards.datatransfer.FileSystemImportWizard;

public class NewAssignmentWizard extends Wizard implements INewWizard {

	private NewAssignmentWizardPage initialPage;
	private AssignmentDescriptionWizardPage descriptionPage;
	private AssignmentDeadlineWizardPage deadlinePage;
	private AssignmentReferencesWizardPage referencePage;

	private ISelection select;
	private IStructuredSelection structuredSelection;
	private IWorkbench workbench;

	private AssignmentCreationOperation assignmentOperation;

	public NewAssignmentWizard() {
		super();
		setNeedsProgressMonitor(true);
	}

	@Override
	public void addPages() {
		initialPage = new NewAssignmentWizardPage(select);
		addPage(initialPage);
		descriptionPage = new AssignmentDescriptionWizardPage(select);
		addPage(descriptionPage);
		deadlinePage = new AssignmentDeadlineWizardPage(select);
		addPage(deadlinePage);
		referencePage = new AssignmentReferencesWizardPage(select);
		addPage(referencePage);

	}

	/**
	 * Displays a dialog showing the exception's message
	 * @param e The exception caught
	 */
	protected void handleException(Exception e) {
		IStatus status = new Status(3, "AssignmentCreationException", -1,
				"An Exception has Occured !", e);
		ErrorDialog error = new ErrorDialog(getShell(), "Error",
				e.getMessage(), status, 0);
		error.create();
		error.open();
	}

	public void init(IWorkbench workbench, IStructuredSelection selection) {

		this.structuredSelection = selection;
		this.workbench = workbench;

	}

	@Override
	public boolean performFinish() {
		getShell().getDisplay().asyncExec(new Runnable() {
			public void run() {

			}
		});
		final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		assignmentOperation = new AssignmentCreationOperation();
		IRunnableWithProgress op = new IRunnableWithProgress() {
			public void run(final IProgressMonitor monitor)
					throws InvocationTargetException {
				try {
					getShell().getDisplay().asyncExec(new Runnable() {
						public void run() {

							monitor.beginTask("Creating Assignment ...", 100);

							try {
								assignmentOperation.createAssignment(root,
										initialPage.getAssignmentName(),
										initialPage.getAssignmentPath(),
										initialPage.getAssignmentDetails(),
										deadlinePage.getDeadlineDetails(),
										descriptionPage
												.getAssignmentDescription(),
										descriptionPage
												.getAssignmentEvaluation(),
										referencePage.getReferences(), monitor);
							} catch (CoreException e) {
								handleException(e);
							}
						}
					});

				} finally {
					monitor.done();
				}
			}
		};

		try {
			new ProgressMonitorDialog(getShell()).run(true, false, op);
		} catch (InterruptedException e) {

		} catch (InvocationTargetException e) {
			Throwable realException = e.getTargetException();
			MessageDialog.openError(getShell(), "Error", realException
					.getMessage());

		}

		startImportWizard();

		return true;
	}

	/**
	 * Opens the Import Wizard Dialog
	 * @see FileSystemImportWizard
	 */
	protected void startImportWizard() {
		MessageDialog msg = new MessageDialog(getShell(), "Import Files", null,
				"Do you want to import files in the assignment ?",
				MessageDialog.QUESTION, new String[] { "YES", "NO" }, 1);

		int answer = msg.open();

		if (answer == 0) {
			FileSystemImportWizard wizard = new FileSystemImportWizard();
			wizard.init(workbench, structuredSelection);
			WizardDialog dialog = new WizardDialog(getShell(), wizard);
			dialog.open();
		}
	}

}
